package com.supcon.exchangedata.service;

import com.sun.javaws.Main;
import com.supcon.exchangedata.pojo.entity.oracle.CtKpiDay;
import com.supcon.exchangedata.pojo.entity.oracle.StatistOtherParametersDay;
import com.supcon.exchangedata.pojo.entity.oracle.StatistOtherParametersMonth;
import com.supcon.exchangedata.repository.master.*;
import com.supcon.exchangedata.repository.slave6.SlaveHjBasicInfo3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/12/20/10:07
 * @Description:
 */
@Service
@Slf4j
public class CommonMethodService {
    @Resource
    private MasterKpiDayRepository masterKpiDayRepository;

    @Resource
    private MasterStatistOtherParametersDayRepository masterStatistOtherParametersDayRepository;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private MasterStatistOtherParametersMonthRepository masterStatistOtherParametersMonthRepository;
    @Resource
    private MasterBaseAreaRepository masterBaseAreaRepository;
    @Resource
    private SlaveHjBasicInfo3 slaveHjBasicInfo3;

    @Resource
    private SlaveCarGpsInfoHis slaveCarGpsInfoHis;

    public void deleteByHis(){
        slaveCarGpsInfoHis.deleteByTypeAndLayerCodeDiy(1L, "1009");
        slaveCarGpsInfoHis.deleteByTypeAndLayerCodeDiy(4L, "4012");
        slaveCarGpsInfoHis.deleteByTypeAndLayerCodeDiy(5L, "5015");
    }

    @Transactional(rollbackFor = Exception.class)
    /**
     * 保存指数到kpi 天表(大屏趋势图使用）
     */
    public void saveOrUpdateKpiDay(String code, String day, String value) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        CtKpiDay byCodeAndDayOfMonthKey = masterKpiDayRepository.findByCodeAndDayOfMonthKey(code, dayOfMonthKey);
        if (byCodeAndDayOfMonthKey == null) {
            byCodeAndDayOfMonthKey = new CtKpiDay();
            byCodeAndDayOfMonthKey.setCode(code);
            byCodeAndDayOfMonthKey.setDayOfMonthKey(dayOfMonthKey);
            byCodeAndDayOfMonthKey.setLastUpdateTime(LocalDateTime.now());
            byCodeAndDayOfMonthKey.setDataType(code);

        }
        byCodeAndDayOfMonthKey.setCurrentValue(value);
        masterKpiDayRepository.save(byCodeAndDayOfMonthKey);


    }


    @Transactional(rollbackFor = Exception.class)
    /**
     * 保存数据到天表
     */
    public void saveOrUpdateStatistOtherParametersDay(Long objId, Long type, String day, String value) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        StatistOtherParametersDay byObjIdAndTypeAndDayOfMonthKey = masterStatistOtherParametersDayRepository.findByObjIdAndTypeAndDayOfMonthKey(objId, type, dayOfMonthKey);
        if (byObjIdAndTypeAndDayOfMonthKey == null) {
            byObjIdAndTypeAndDayOfMonthKey = new StatistOtherParametersDay();
            byObjIdAndTypeAndDayOfMonthKey.setObjId(objId);
            byObjIdAndTypeAndDayOfMonthKey.setType(type);
            byObjIdAndTypeAndDayOfMonthKey.setDayOfMonthKey(dayOfMonthKey);
            byObjIdAndTypeAndDayOfMonthKey.setObjType(1L);
            byObjIdAndTypeAndDayOfMonthKey.setLastTime(LocalDateTime.now());
            byObjIdAndTypeAndDayOfMonthKey.setInAvgValue(Double.valueOf(value));
            masterStatistOtherParametersDayRepository.save(byObjIdAndTypeAndDayOfMonthKey);
        } else {
            masterStatistOtherParametersDayRepository.updateDay(Double.valueOf(value), objId, dayOfMonthKey, type);
        }


    }

    public void saveCurrentValue() {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();

        //智能气表覆盖率
        StatistOtherParametersMonth monthValue2 = masterStatistOtherParametersMonthRepository.findTopByObjIdAndTypeOrderByMonthKeyDesc(3L, 30023L);

        if (monthValue2 != null && !StringUtils.isEmpty(monthValue2.getInAvgValue())) {
            Double inAvgValue = monthValue2.getInAvgValue();
            saveRedis(inAvgValue.toString(), "%", timeFormatter.format(now), "10000022999");
        }
        //智能水表覆盖率
        StatistOtherParametersMonth monthValue1 = masterStatistOtherParametersMonthRepository.findTopByObjIdAndTypeOrderByMonthKeyDesc(2L, 20038L);

        if (monthValue1 != null && !StringUtils.isEmpty(monthValue1.getInAvgValue())) {
            Double inAvgValue = monthValue1.getInAvgValue();
            saveRedis(inAvgValue.toString(), "%", timeFormatter.format(now), "10000022872");
        }

        //供水泵站  2006
        Long stationCount0 = masterBaseAreaRepository.countAllByAreaType(2006L);
        if (stationCount0 > 0) {
            saveRedis(stationCount0.toString(), "个", timeFormatter.format(now), "10000022874");
        }


        //排水泵站 2009
        Long stationCount = masterBaseAreaRepository.countAllByAreaType(2009L);
        if (stationCount > 0) {
            saveRedis(stationCount.toString(), "个", timeFormatter.format(now), "10000022878");
        }


        //公交运营里程 10004
        StatistOtherParametersDay dayValue1 = masterStatistOtherParametersDayRepository.findTopByObjIdAndTypeOrderByDayOfMonthKeyDesc(1L, 10004L);
        if (dayValue1 != null && dayValue1.getInAvgValue() != null) {
            Double inAvgValue = dayValue1.getInAvgValue();
            BigDecimal bigDecimal = new BigDecimal(inAvgValue);
            BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP);
            saveRedis(divide.toString(), "万公里", timeFormatter.format(now), "10000022153");
        }

        //自行车还车量 10005
        StatistOtherParametersDay dayValue2 = masterStatistOtherParametersDayRepository.findTopByObjIdAndTypeOrderByDayOfMonthKeyDesc(1L, 10005L);
        if (dayValue2 != null && dayValue2.getInAvgValue() != null) {
            Double inAvgValue = dayValue2.getInAvgValue();
            BigDecimal bigDecimal = new BigDecimal(inAvgValue);
            BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP);
            saveRedis(divide.toString(), "万辆", timeFormatter.format(now), "10000022355");
        }

        //自行车租车 10006
        StatistOtherParametersDay dayValue3 = masterStatistOtherParametersDayRepository.findTopByObjIdAndTypeOrderByDayOfMonthKeyDesc(1L, 10006L);
        if (dayValue3 != null && dayValue3.getInAvgValue() != null) {
            Double inAvgValue = dayValue3.getInAvgValue();
            BigDecimal bigDecimal = new BigDecimal(inAvgValue);
            BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP);
            saveRedis(divide.toString(), "万辆", timeFormatter.format(now), "10000022154");
        }
        //电子站牌数 10000022860 1009
        Long stationCount1 = masterBaseAreaRepository.countAllByAreaType(1023L);
        if (stationCount1 > 0) {
            saveRedis(stationCount1.toString(), "个", timeFormatter.format(now), "10000022860");
        }

        //公交站点数 	10000022862
        Long stationCount3 = masterBaseAreaRepository.countAllByAreaType(1001L);
        if (stationCount3 > 0) {
            saveRedis(stationCount3.toString(), "个", timeFormatter.format(now), "10000022862");
        }
        //清运线路数
        Long stationCount4 = slaveHjBasicInfo3.countAllByTongidIsNotNull();
        if (stationCount4 > 0) {
            saveRedis(stationCount4.toString(), "条", timeFormatter.format(now), "10000022895");
        }
        //养护基地
        Long stationCount5 = masterBaseAreaRepository.countAllByAreaType(5017L);
        if (stationCount5 > 0) {
            saveRedis(stationCount5.toString(), "个", timeFormatter.format(now), "10000022905");
        }
        //填报基地 1004
        Long stationCount6 = masterBaseAreaRepository.countAllByAreaType(1004L);
        if (stationCount6 > 0) {
            saveRedis(stationCount6.toString(), "个", timeFormatter.format(now), "10000022859");
        }

        //供水
        StatistOtherParametersDay dayValue4 = masterStatistOtherParametersDayRepository.findTopByObjIdAndTypeOrderByDayOfMonthKeyDesc(2L, 20001L);
        if (dayValue4 != null && dayValue4.getInAvgValue() != null) {
            Double inAvgValue = dayValue4.getInAvgValue();
            BigDecimal bigDecimal = new BigDecimal(inAvgValue);
            BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP);
            saveRedis(divide.toString(), "万吨", timeFormatter.format(now), "10000022150");
        }

        //污水
        StatistOtherParametersDay dayValue5 = masterStatistOtherParametersDayRepository.findTopByObjIdAndTypeOrderByDayOfMonthKeyDesc(2L, 20004L);
        if (dayValue5 != null && dayValue5.getInAvgValue() != null) {
            Double inAvgValue = dayValue5.getInAvgValue();
            BigDecimal bigDecimal = new BigDecimal(inAvgValue);
            BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP);
            saveRedis(divide.toString(), "万吨", timeFormatter.format(now), "10000022157");
        }
        //原水
        StatistOtherParametersDay dayValue6 = masterStatistOtherParametersDayRepository.findTopByObjIdAndTypeOrderByDayOfMonthKeyDesc(2L, 20035L);
        if (dayValue6 != null && dayValue6.getInAvgValue() != null) {
            Double inAvgValue = dayValue6.getInAvgValue();
            BigDecimal bigDecimal = new BigDecimal(inAvgValue);
            BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP);
            saveRedis(divide.toString(), "万吨", timeFormatter.format(now), "10000022876");
        }
        //清运点 4009
        Long stationCount2 = masterBaseAreaRepository.countAllByAreaType(4009L);
        if (stationCount2 > 0) {
            saveRedis(stationCount2.toString(), "个", timeFormatter.format(now), "10000022899");
        }
    }

    public void saveRedis(String current, String unit, String dateTimeStr, String code) {
        LinkedHashMap<String, String> map1 = new LinkedHashMap<>();
        map1.put("current", current);
        map1.put("unit", unit);
        map1.put("saveTime", dateTimeStr);
        redisTemplate.opsForValue().set(code, map1);
    }
}
